package com.yuanqi.controller;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuanqi.common.Result;
import com.yuanqi.dto.ProductQueryDTO;
import com.yuanqi.pojo.Product;
import com.yuanqi.service.ProductService;
import com.yuanqi.mapper.ProductMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 商品管理接口
 */
@RestController
@RequestMapping("/api/product")
@RequiredArgsConstructor
public class ProductController {

    private final ProductService productService; // 注入服务
    private final ProductMapper productMapper;   // 注入Mapper以调用自定义SQL

    @PostMapping
    public Result<Boolean> create(@RequestBody Product product) {
        // 设置默认值
        if (product.getStock() == null) {
            product.setStock(0);
        }
        if (product.getStatus() == null) {
            product.setStatus(1); // 默认上架
        }
        if (product.getEnableBatch() == null) {
            product.setEnableBatch(0); // 默认不启用批次管理
        }
        if (product.getEnableSn() == null) {
            product.setEnableSn(0); // 默认不启用序列号管理
        }
        if (product.getImageUrl() == null) {
            product.setImageUrl(""); // 默认空图片
        }

        boolean ok = productService.save(product);
        return Result.success(ok);
    }

    @PutMapping("/{id}")
    public Result<Boolean> update(   @PathVariable Long id,@RequestBody Product product) {
        product.setId(id);
        boolean ok = productService.updateById(product);
        return Result.success(ok);
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean ok = productService.removeById(id);
        return Result.success(ok);
    }

    @GetMapping("/page")
    public Result<Page<Product>> page(
            @RequestParam(required = false) String productName,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        QueryWrapper<Product> qw = new QueryWrapper<>();
        if (StringUtils.isNotBlank(productName)) {
            qw.like("product_name", productName);
        }
        if (categoryId != null) {
            qw.eq("category_id", categoryId);
        }
        if (startTime != null) {
            qw.ge("created_at", startTime);
        }
        if (endTime != null) {
            qw.le("created_at", endTime); // 截止时间
        }
        qw.orderByDesc("created_at"); // 按时间倒序
        // 使用自定义联表 SQL 查询，带出分类名与品牌名
        Page<Product> page = new Page<>(pageNo, pageSize);
        com.baomidou.mybatisplus.core.metadata.IPage<Product> result = productMapper.selectPageWithNames(page, productName, categoryId, startTime, endTime);
        return Result.success((Page<Product>) result);
    }

    @GetMapping("/{id}")
    public Result<Product> detail(@PathVariable Long id) {
        return Result.success(productService.getById(id)); // 查询详情
    }
}


